<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/../common.php';
ini_set ("memory_limit", "1000M");

$image_dir = 'M:/';
$image_dir = __dir__.'/res.image';
$demo = new Demo('res.reply.txt', 'res.username.txt', 'res.itemname.txt', $image_dir);

$p_register = 1;
$p_publish_item = $p_register*2;
$p_reply_item = $p_publish_item*5;
$p_rate_item = $p_publish_item*5;
$p_circle_item = $p_publish_item*3;

$total = $p_register+$p_publish_item+$p_Reply_item+$p_rate_item+$p_circle_item;

for ($i=0; $i<1000; $i++)
{
	if (count($demo->demoUsers) > 10000)break;
	
	if (trigger_by_percent($p_register/$total))
		$demo->doRegisterUser();
	
	if (trigger_by_percent($p_publish_item/$total))
		$demo->doPublishItem();

	if (trigger_by_percent($p_reply_item/$total))
		$demo->doReplyItem();
	
	if (trigger_by_percent($p_rate_item/$total))
		$demo->doRateItem();
	
	if (trigger_by_percent($p_circle_item/$total))
		$demo->doCircleItem();
}
//print_r($data->userNames);

function trigger_by_percent($p)
{
	$b = 10000000;
	return mt_rand(0, $b) < $p*$b;
}

class Demo
{
	var $rates = array('yes', 'no');
	var $email_com_array = array('sina.com', 'gmail.com', 'hotmail.com', '163.com');
	var $replys = array();
	var $userNames = array();
	var $itemNames = array();
	var $itemImages = array();

	var $dirImage = "";

	var $demoUsers = array();
	var $demoItems = array();
	
	function Demo($txtReply, $txtUserName, $txtItemName, $dirImage)
	{
		$this->replys = file($txtReply);
		$this->userNames = file($txtUserName);
		shuffle($this->userNames);
		$this->itemNames = file($txtItemName);
		$this->dirImage = $dirImage;

		$this->file_array($dirImage);
		/*print_r(array_slice($this->itemImages, 0, 100));
		echo count($this->itemImages);exit;

		foreach ($this->file_array($dirImage) as $name)
		{
			$this->itemImages[] = $name;
		}
		echo count($this->itemImages);exit;*/
		
/*	for ($i=0; $i<100; $i++)
	{
		$comment = self::getRandItem($this->replys);
		echo $comment."\n";
	}
		
		exit;*/

	}
	
	function doRegisterUser()
	{
		$user_nickname = trim(array_shift($this->userNames));
		$user_email = strtolower(str_replace(" ", "_", $user_nickname)).'@'
			.self::getRandItem($this->email_com_array);
		$user_passwd = md5($user_email);
		
		$user_id = user::register($user_email, $user_nickname, $user_passwd);
		if ($user_id)
		{
			$this->demoUsers[$user_id] = $user_nickname;
			$this->actionLog("[Register]", $user_nickname);
			return true;
		}
		
		return false;
	}
	
	function doPublishItem()
	{
		if (!count($this->demoUsers))
		{
			return false;
		}
		
		$user_id = array_rand($this->demoUsers);
		$image = self::getRandItem($this->itemImages);
		$t = explode('/', $image);
		$image_name = array_pop($t);

		$item_info = array();
		$item_info['item_name'] = self::getRandItem($this->itemNames);
		$item_info['item_image'] = $image_name;
		$item_info['user_id'] = $user_id;
		$item_info['item_emotion'] = self::getRandItem($this->replys);

		$item_id = item::add($item_info, true);
		
		if (!$item_id)
		{
			return false;
		}
		//echo $this->dirImage."/".$image_name;exit;
		itemimage::save($item_id, $image);
		$this->demoItems[$item_id] = $item_info;
		
		$this->actionLog("[Publish]",
			$this->demoUsers[$user_id], $item_info['item_name']);
		return true;
	}
	
	function doReplyItem()
	{
		if (!count($this->demoUsers) || !count($this->demoItems))
		{
			return false;
		}
		
		$item_id = array_rand($this->demoItems);
		$user_id = array_rand($this->demoUsers);
		$user_nickname = $this->demoUsers[$user_id];
		$reply = self::getRandItem($this->replys);

		$item_id = item::reply($item_id, $reply, $user_id, $user_nickname);
		
		if (!$item_id)
		{
			return false;
		}

		$this->actionLog("[Reply]",
			$this->demoUsers[$user_id], $item_info['item_name']);
		return true;
	}
	
	function doRateItem()
	{
		if (!count($this->demoUsers) || !count($this->demoItems))
		{
			return false;
		}
		
		$item_id = array_rand($this->demoItems);
		$user_id = array_rand($this->demoUsers);
		$user_nickname = $this->demoUsers[$user_id];
		$rate = self::getRandItem($this->rates);

		$rate_id = item::rate($item_id, $rate, $user_id, $user_nickname);
		
		if (!$rate_id)
		{
			return false;
		}

		$this->actionLog("[Rate]",
			$this->demoUsers[$user_id], $rate, $item_info['item_id']);
		return true;
	}
	
	function doCircleItem()
	{
		if (!count($this->demoUsers) || !count($this->demoItems))
		{
			return false;
		}
		
		$user_id = array_rand($this->demoUsers);
		$user_nickname = $this->demoUsers[$user_id];
		$item_id = array_rand($this->demoItems);
		$comment = self::getRandItem($this->replys);
		
		if (!$comment) return false;
		
		$x = mt_rand(0, 10000)/10000;
		$y = mt_rand(0, 10000)/10000;

		$circle_id = item::circle($item_id, $x, $y, $comment, $user_id, $user_nickname);
		
		if (!$circle_id)
		{
			return false;
		}

		$this->actionLog("[Circle]",
			$this->demoUsers[$user_id], $item_id, $x, $y, $comment, $item_info['item_id']);
		return true;
	}
	
	function file_array($path, $exclude = ".|..", $recursive = true) {
	    $path = rtrim($path, "/") . "/";
	    $folder_handle = opendir($path);
	    $exclude_array = explode("|", $exclude);
	    $result = array();
	    while(false !== ($filename = readdir($folder_handle))) {
	        if(!in_array(strtolower($filename), $exclude_array)) {
	            if(is_dir($path . $filename . "/")) {
	                if($recursive) $result[$filename] = $this->file_array($path . $filename, $exclude, true);
	            } else {
	                $this->itemImages[] = $path . $filename;
	            }
	        }
	    }
	    return $result;
	}
	
	function getRandItem($a)
	{
		for ($i=0; $i<10; $i++)
		{
			$s = $a[mt_rand(0, count($a))];
			if ($s) return trim($s);
		}
	}
	
	function actionLog()
	{
		foreach (func_get_args() as $s)
			echo $s."\t";
		echo "\n";
	}

}